<?php
class Apanel extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('cismarty');
		$this->load->model("apanel/template");
		$this->load->model("apanel/db_log");
		//$this->load->library('pagination');
	}
	
	public function index(){
		$fail	= "";
		if($this->input->post('btnlogin') != null){
			$user 	= $this->input->post('username');
			$pass	= $this->input->post("password");
			if($user != "" && $pass != "" ){
				$pass = md5($pass);
				$sql = "select * from hotel_user where username=? and password=?";
				$res = $this->db->query($sql,array($user,$pass));
				$num = $res->num_rows();
				if($num > 0){
					// create session
					$hotel = $res->row();
					$ss = array("sess_login"=>'y',
							"sess_username"=>$hotel->username,
							"sess_userid"=>$hotel->pkid,
							"sess_hname"=>$hotel->name,
							"sess_dp"=>$hotel->department_pkid
					);
					$this->session->set_userdata($ss);
					$this->db_log->logs();
					redirect('apanel/dashboard');
				}		
			}else {
				$fail  = "Please insert Username,Password";
				$this->cismarty->assign("errors",$fail);
			}
		} 
		//== template
		$this->cismarty->assign("form_open",form_open('apanel/index','class="mws-form"'));
		$this->cismarty->assign("form_close",form_close());
		$this->cismarty->assign("url",base_url());
		$this->cismarty->display('apanel/login.html');
	}
	
	public function logout(){
		$this->session->unset_userdata("sess_login");
		redirect("apanel","refresh");
		exit();
	}
	
	public function dashboard(){
		if($this->session->userdata('sess_login') == null) 
				redirect('apanel');
		//== template
		$this->cismarty->assign("header",$this->template->header());
		$this->cismarty->assign("menu",$this->template->menu(1));
		$this->cismarty->assign("footer",$this->template->footer());
		$this->cismarty->assign("url",base_url());
		$this->cismarty->display('apanel/dashboard.html');
	}
}
?>
